package com.hand.demo.api.controller.v1;

import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.hand.demo.app.service.HhrTaCategoryService;
import com.hand.demo.domain.entity.HhrTaCategory;
import com.hand.demo.domain.repository.HhrTaCategoryRepository;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 标签分类表(HhrTaCategory)表控制层
 *
 * @author
 * @since 2025-07-02 11:10:34
 */

@RestController("hhrTaCategoryController.v1")
@RequestMapping("/v1/{organizationId}/hhr-ta-categorys")
public class HhrTaCategoryController extends BaseController {

    @Autowired
    private HhrTaCategoryRepository hhrTaCategoryRepository;

    @Autowired
    private HhrTaCategoryService hhrTaCategoryService;

    @ApiOperation(value = "标签分类表列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping
    public ResponseEntity<Page<HhrTaCategory>> list(HhrTaCategory hhrTaCategory, @PathVariable Long organizationId,
                                                    @ApiIgnore @SortDefault(value = HhrTaCategory.FIELD_ID,
                                                            direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<HhrTaCategory> list = hhrTaCategoryService.selectList(pageRequest, hhrTaCategory);
        return Results.success(list);
    }

    @ApiOperation(value = "标签分类表明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{id}/detail")
    public ResponseEntity<HhrTaCategory> detail(@PathVariable Long id) {
        HhrTaCategory hhrTaCategory = hhrTaCategoryRepository.selectByPrimary(id);
        return Results.success(hhrTaCategory);
    }

    @ApiOperation(value = "创建或更新标签分类表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<List<HhrTaCategory>> save(@PathVariable Long organizationId, @RequestBody List<HhrTaCategory> hhrTaCategorys) {
        validObject(hhrTaCategorys);
        SecurityTokenHelper.validTokenIgnoreInsert(hhrTaCategorys);
        hhrTaCategorys.forEach(item -> item.setTenantId(organizationId));
        hhrTaCategoryService.saveData(hhrTaCategorys);
        return Results.success(hhrTaCategorys);
    }

    @ApiOperation(value = "删除标签分类表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<?> remove(@RequestBody List<HhrTaCategory> hhrTaCategorys) {
        SecurityTokenHelper.validToken(hhrTaCategorys);
        hhrTaCategoryRepository.batchDeleteByPrimaryKey(hhrTaCategorys);
        return Results.success();
    }

}

